package thread;

import java.util.concurrent.*;

public class Demo25 {

    public static void main(String[] args) {
        //ExecutorService threadPool =  Executors.newCachedThreadPool();
        ExecutorService threadPool = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 100; i++) {
            int id = i;
            threadPool.submit(()->{
                System.out.println("打印"+id+Thread.currentThread().getName());
            });

        }
        //threadPool.shutdown();//关闭所有线程，但不能保证所有任务执行完,可以调用awaitTermination方法
        try {
            if(!threadPool.awaitTermination(10, TimeUnit.SECONDS)){
                threadPool.shutdown();
            }else{
                threadPool.shutdown();
            }
        }catch (InterruptedException e) {
                threadPool.shutdownNow();
        }
    }
}
